import { Injectable } from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {API} from "../../../constants/constants";
import {Observable} from "rxjs";
import {MsgResult} from "../../../models/msg-result";

@Injectable()
export class UsersService {

  constructor(
    private requestUtil : RequestUtil
  ) { }

  /**
   *  无参查询初始化
   * @returns {Observable<MsgResult>|any}
   */
  getUsers():Observable<MsgResult>{
    return this.requestUtil.get(API.getUsers);
  }

  /**
   *  添加用户
   * @param user
   * @returns {Observable<MsgResult>|any}
   */
  insertUser(user):Observable<MsgResult>{
    let params="name="+user.name+"&phone="+user.phone+"&duty="+user.duty +"&orgId="+user.orgId
      +"&depId="+user.depId+"&type="+user.type+"&regionId="+user.regionId;
    return this.requestUtil.post(API.insertUser,params);
  }

  /**
   * 批量删除
   * @param ids
   * @returns {Observable<MsgResult>|any}
   */
  deleteUsers(ids , userCodes):Observable<MsgResult>{
    let params="userIdStr="+ids+"&userCodes="+userCodes;
    return this.requestUtil.post(API.deleteUsers,params);
  }

  /**
   * 根据id查询
   * @param id
   * @returns {Observable<MsgResult>|any}
   */
  getUserById(id ):Observable<MsgResult>{
    let params="userId="+id;
    return this.requestUtil.get(API.getUserById,params);
  }

  /**
   *  修改用户
   * @param user
   * @returns {Observable<MsgResult>|any}
   */
  updateUser(user):Observable<MsgResult>{
    let params="userId="+user.userId+"&name="+user.name+"&phone="+user.phone+"&duty="+user.duty
      +"&orgId="+user.orgId+"&depId="+user.depId+"&type="+user.type+"&regionId="+user.regionId;
    return this.requestUtil.post(API.updateUser,params);
  }

  /**
   *  修改用户，分配账户
   * @param user
   * @returns {Observable<MsgResult>|any}
   */
  updateUserForAssignAcc(userId,userCode,accountId):Observable<MsgResult>{
    let params="userId="+userId+"&userCode="+userCode+"&accountId="+accountId;
    return this.requestUtil.post(API.updateUser,params);
  }

  /**
   * 根据userCode创建账户接口
   * @param userCode
   * @returns {Observable<MsgResult>|any}
   */
  createAccountByUserCode(account):Observable<MsgResult>{
    let params="userCode="+account.accountName;
    return this.requestUtil.post(API.createAccountByUserCode,params);
  }

  /**
   * 获取行政区划关联树
   * @param userCode
   * @returns {Observable<MsgResult>|any}
   */
  getRetrieveRegionsTree(): Observable<MsgResult> {
    return this.requestUtil.get(API.getRetrieveRegionsTree);
  }

  /**
   * 根据行政区划编码;
   * 获取其下的企业/机构关联树
   */
  getRetrieveOrganizations(regionCode): Observable<MsgResult> {
    let params="regionCode="+regionCode;
    return this.requestUtil.get(API.getRetrieveOrganizations,params);
  }

  /**
   * 根据企业/政府id;
   * 获得其下的部门与子部门的关联树
   */
  retrieveDeptsByOrgId(orgId): Observable<MsgResult> {
    let params="orgId="+orgId;
    return this.requestUtil.get(API.retrieveDeptsByOrgId,params);
  }

  /**
   * 获取用户信息
   */
  getCurLoginAccountInfo(): Observable<MsgResult> {
    return this.requestUtil.get(API.getCurLoginAccountInfo);
  }
}
